import "../style/register.css";
import { EUV } from "@hongweifei/euv";
import { register } from "../api/user";
import { StatusCode } from "../api/web-result";

new EUV({
    elementSelector: "#app",
    mounted() {

    },

    data: {
        formData: {
            username: "",
            email: "",
            password: "",
            password2: ""
        }
    },

    methods: {
        gotoIndex() {
            window.location.href = "../index.html";
        },
        gotoLogin() {
            window.location.href = "./login.html";
        },

        async submitRegister(e) {
            e.preventDefault();
            const { username, email, password, password2 } = this.data.formData;
            if (password !== password2) {
                alert("两次密码不一致");
                return;
            }
            const result = await register({
                username,
                email,
                password
            });
            alert(result.message);
            if (result.code === StatusCode.SUCCESS) {
                window.location.href = "./login.html";
            }
        }
    }
});

